//
//  WXPAuthBankCardHeaderView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/3.
//

#import "WXPAuthBankCardHeaderView.h"

@implementation WXPAuthBankCardHeaderView

#pragma mark - lazy
- (UIView *)viewContent{
    if (!_viewContent) {
        _viewContent = [UIView new];
        _viewContent.backgroundColor = COLOR_WHITE;
        _viewContent.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    }
    return _viewContent;
}

// 银行名称
- (WXPAuthSelectView *)bankNameView
{
    if (_bankNameView == nil) {
        _bankNameView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Nombre del banco";
        [_bankNameView resetViewWithModel:model];
        _bankNameView.tfContent.font = [UIFont systemFontOfSize:14];
        WEAKSELF
        _bankNameView.blockSelect = ^{
            [GlobalMethod buryPointWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Name action:KEY_GET_FOCUS type:KEY_select];
            if(weakSelf.type == 1){
                [GlobalMethod hitPointWithPageName:KEY_VerifyBankcard action:KEY_Bank_Name_Focus extInfo:nil];
            }else{
                [GlobalMethod hitPointWithPageName:KEY_EditBankcard action:KEY_Bank_Name_Focus extInfo:nil];
            }
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window endEditing:true];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 0; i < weakSelf.bankNameAry.count; i ++) {
                NSDictionary * dic = weakSelf.bankNameAry[i];
                ModelBtn * model = [ModelBtn new];
                model.title = [dic stringValueForKey:KEY_bankName];
                model.content = [dic stringValueForKey:KEY_bankCode];
                [array addObject:model];
            }
            weakSelf.pickerView.tfSearch.text = @"";
            [weakSelf.pickerView resetViewWithAry:array withTag:1];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _bankNameView;
}
// 银行卡号
- (WXPAuthSelectView *)tfBankCard
{
    if (_tfBankCard == nil) {
        _tfBankCard = [WXPAuthSelectView  new];
        _tfBankCard.tfContent.keyboardType = UIKeyboardTypeNumberPad;
        _tfBankCard.tfContent.font = [UIFont systemFontOfSize:14];
        _tfBankCard.imageArrow.hidden = true;
        _tfBankCard.tfContent.userInteractionEnabled = true;
        ModelBtn * model = [ModelBtn new];
        model.title = @"Número de cuenta CLABE o tarjeta bancaria";
        model.content = @"Por favor escribe";
        [_tfBankCard resetViewWithModel:model];
        _tfBankCard.tfContent.delegate = self;
        WEAKSELF
//        _tfBankCard.blockTextFieldDidChange = ^(BOOL isInput) {
//            [weakSelf verifyBankCard];
//            [weakSelf verifyConfirmarBankCard];
//            
//            // 这个类里面两个LRBMailTextField，没法直接用isInput区分了
////            if(isInput){
////                [GlobalMethod userBehaviorTrackWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Card_Input1 action:KEY_INPUT type:KEY_input];
////            }
//            // 区分是输入
//            static NSUInteger previousLength = 0;
//            NSUInteger currentLength = weakSelf.tfBankCard.contentTextField.text.length;
////            if (currentLength > previousLength || isInput) {
//            if (isInput) {
//                [GlobalMethod userBehaviorTrackWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Card_Input1 action:KEY_INPUT type:KEY_input];
//            }
//            previousLength = currentLength;
//            
//            
//            [weakSelf.tfBankCard.contentTextField becomeFirstResponder];
//        };
        
//        _tfBankCard.blockTextFieldDidEndEditing = ^{
//            [GlobalMethod userBehaviorTrackWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Card_Input1 action:KEY_LOST_FOCUS type:KEY_input];
//        };
//        _tfBankCard.blockCharactersInRange = ^{
//            [GlobalMethod userBehaviorTrackWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Card_Input1 action:KEY_PARSE type:KEY_input];
//        };
    }
    return  _tfBankCard;
}
- (WXPAuthPickerView *)pickerView
{
    if (_pickerView == nil) {
        _pickerView = [WXPAuthPickerView  new];
        [_pickerView resetViewWithIsShowSearch:true];
        WEAKSELF
        _pickerView.blockDidSelectRow = ^(ModelBtn *model, NSInteger tag) {
            if(tag == 1){
                [GlobalMethod buryPointWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Name action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.bankNameView.tfContent.text = model.title;
                    weakSelf.bankCode = model.content;
                }
            }
            if(weakSelf.blockResetAuthBtn){
                weakSelf.blockResetAuthBtn();
            }
        };
    }
    return  _pickerView;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = frame.size.width > 0 ? frame.size.width : SCREEN_WIDTH;
        [self configUI];
    }
    return self;
}

#pragma mark - UITextField delegate
// 获得焦点
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if(self.type == 1){
        [GlobalMethod buryPointWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Card_Input1 action:KEY_GET_FOCUS type:KEY_input];
        [GlobalMethod hitPointWithPageName:KEY_VerifyBankcard action:KEY_CLABE1_Focus extInfo:nil];
    }else{
        [GlobalMethod hitPointWithPageName:KEY_EditBankcard action:KEY_CLABE1_Focus extInfo:nil];
    }
}
// 失去焦点
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if(self.type == 1){
        [GlobalMethod buryPointWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Card_Input1 action:KEY_LOST_FOCUS type:KEY_input];
    }
}

// 粘贴行为
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(self.type == 1){
        if (isStr(string)) {
            [GlobalMethod buryPointWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Card_Input1 action:KEY_INPUT type:KEY_input];
        }
    }
    // 检测是否为粘贴行为
    NSString * trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([UIPasteboard generalPasteboard].string.length > 0 && [trimmedString isEqualToString:[UIPasteboard generalPasteboard].string]) {
        if(self.type == 1){
            [GlobalMethod buryPointWithPageName:KEY_VerifyBankcard featureName:KEY_Bank_Card_Input1 action:KEY_PARSE type:KEY_input];
        }
        return true;
    }
    return YES;
}

- (void)configUI{
    self.viewContent = [UIView initWithViews:@[
        self.bankNameView,
        self.tfBankCard,
//        [GlobalMethod getViewWithHeight:Ratio(16)],
    ] width:SCREEN_WIDTH-2*LEFT_MARDIN];
    self.viewContent.backgroundColor = COLOR_WHITE;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.viewContent.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(16, 16)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    self.viewContent.layer.mask = maskLayer;
    self.viewContent.centerXTop = XY(self.width/2, 0);
    [self addSubview:self.viewContent];
    self.height = self.viewContent.bottom;
}

@end
